package com.example.qqviewspringboot.controller;

import com.example.qqviewspringboot.data.Notification;
import com.example.qqviewspringboot.repository.NotificationRepository;
import com.example.qqviewspringboot.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/notifications")
public class NotificationController {

    @Autowired
    private NotificationRepository notificationRepository;

    @Autowired
    private NotificationService notificationService;

    /**
     * 获取用户未读通知
     */
    @GetMapping("/{userId}")
    public ResponseEntity<List<Notification>> getUnreadNotifications(@PathVariable Long userId) {
        // 查询未读通知
        List<Notification> notifications = notificationRepository.findByRecipientIdAndIsRead(userId, false);
        return ResponseEntity.ok(notifications);
    }

    /**
     * 将通知标记为已读
     */
    @PostMapping("/{notificationId}/read")
    public ResponseEntity<String> markAsRead(@PathVariable Long notificationId) {
        // 查找通知
        Notification notification = notificationRepository.findById(notificationId)
                .orElseThrow(() -> new IllegalArgumentException("Notification not found"));

        // 标记为已读
        notification.setRead(true);
        notificationRepository.save(notification);

        return ResponseEntity.ok("Notification marked as read");
    }

    /**
     * 获取当前用户的好友申请通知
     */
    @GetMapping("/friend-requests")
    public ResponseEntity<List<Notification>> getFriendRequestNotifications(@RequestParam Long userId) {
        // 使用 NotificationService 获取好友请求通知
        List<Notification> notifications = notificationService.getFriendRequestNotifications(userId);
        return ResponseEntity.ok(notifications);
    }
}
